package com.yystar.support.util;

import java.math.BigDecimal;

public abstract class WMathUtility {
	public static double floor(BigDecimal b, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		return b.divide(BigDecimal.valueOf(1), scale, BigDecimal.ROUND_FLOOR).doubleValue();
	}

	public static double round(BigDecimal b, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		return b.divide(BigDecimal.valueOf(1), scale, BigDecimal.ROUND_HALF_DOWN).doubleValue();
	}

	public static double roundDown(BigDecimal b, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		return b.divide(BigDecimal.valueOf(1), scale, BigDecimal.ROUND_DOWN).doubleValue();
	}

	public static double roundUp(BigDecimal b, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		return b.divide(BigDecimal.valueOf(1), scale, BigDecimal.ROUND_UP).doubleValue();
	}
	
	/**
	 * Round
	 * @param v
	 * @param scale
	 * @return
	 */
	public static double round(double v, int scale) {
		if (Double.isInfinite(v) || Double.isNaN(v)) {
			return v;
		}
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, BigDecimal.ROUND_HALF_DOWN).doubleValue();
	}
	
	/**
	 * Floor
	 * @param v
	 * @param scale
	 * @return
	 */
	public static double floor(double v, int scale) {
		if (Double.isInfinite(v) || Double.isNaN(v)) {
			return v;
		}
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, BigDecimal.ROUND_FLOOR).doubleValue();
	}

}
